package com.yanze.customviewdemo.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposePathEffect;
import android.graphics.CornerPathEffect;
import android.graphics.DashPathEffect;
import android.graphics.DiscretePathEffect;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathDashPathEffect;
import android.graphics.PathEffect;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.SumPathEffect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

public class Bezier extends View {
    public static final String TAG = "yanze_bezier";

    private Paint mPaint;
    private int centerX, centerY;
    private PointF start, end, control;

    public Bezier(Context context) {
        super(context);
    }

    public Bezier(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setColor(Color.BLACK);
        mPaint.setStrokeWidth(8);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setTextSize(60);

        start = new PointF(0, 0);
        end = new PointF(0, 0);
        control = new PointF(0, 0);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Log.i(TAG, "onSizeChanged: ");
        centerX = w / 2;
        centerY = h / 2;

        // 初始化数据点和控制点位置
        start.x = centerX - 200;
        start.y = centerY;
        end.x = centerX + 200;
        end.y = centerY;
        control.x = centerX;
        control.y = centerY - 100;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.i(TAG, "onDraw: ");
        // 绘制数据点和控制点
        mPaint.setColor(Color.GRAY);
        mPaint.setStrokeWidth(20);
        canvas.drawPoint(start.x, start.y, mPaint);
        canvas.drawPoint(end.x, end.y, mPaint);
        canvas.drawPoint(control.x, control.y, mPaint);

        // 绘制辅助线
        mPaint.setStrokeWidth(4);
        canvas.drawLine(start.x, start.y, control.x, control.y, mPaint);
        canvas.drawLine(control.x, control.y, end.x, end.y, mPaint);

        // 绘制贝塞尔曲线
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(8);

        Path path = new Path();
        path.moveTo(start.x, start.y);
        path.quadTo(control.x, control.y, end.x, end.y);

        canvas.drawPath(path,mPaint);
//
//        canvas.drawColor();
//        canvas.drawRGB();
//        canvas.drawARGB();
//
//        canvas.drawPoint();
//        canvas.drawPoints();
//        canvas.drawLine(startX,startY,stopX,stopY,paint);
//        canvas.drawLines();
//        canvas.drawRect();
//        canvas.drawRoundRect(new RectF(1,2,3,4),1,2,paint);
//        canvas.drawRoundRect(1,2,3,4,5,6,paint);
//        canvas.drawOval();
//        canvas.drawCircle();
//        canvas.drawArc();
//        canvas.drawBitmap();
//        canvas.drawPicture();
//
//        canvas.drawText();
//        canvas.drawTextOnPath();
//        canvas.drawPosText();
//
//        canvas.drawPath();
//
//        canvas.drawVertices();
//        canvas.drawBitmapMesh();
//
//        canvas.clipPath();
//        canvas.clipRect();
//        canvas.clipOutPath();
//
//        canvas.save(); // 保存当前状态
//        canvas.restore(); // 回滚到上一次保存的状态
//        canvas.saveLayer(); // 保存图层状态
//        canvas.saveLayerAlpha()
//        canvas.restoreToCount(); //  回滚到指定状态
//        canvas.getSaveCount(); //  获取保存次数
//
//        canvas.translate();
//        canvas.scale();
//        canvas.rotate();
//        canvas.skew();
//
//        canvas.setMatrix(Matrix matrix);
//        canvas.getMatrix();
//        canvas.concat(Matrix matrix);
//
//        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
//        paint.setColor();
//        paint.setARGB();
//        paint.setColorFilter();
//        paint.setShader();
//        paint.setXfermode();
//
//        paint.setAntiAlias();
//        paint.setStyle();
//        paint.setStrokeCap(Paint.Cap);
//        paint.setStrokeWidth();
//
//        paint.setDither(boolean dither);
//        paint.setFilterBitmap(filter);
//
//        PathEffect corner = new CornerPathEffect(20);//
//        PathEffect discrete = new DiscretePathEffect(20,0);
//        PathEffect dash = new DashPathEffect();
//        Path shapePath = Path();
//        PathEffect pathDash = new PathDashPathEffect(shapePath,40,0, PathDashPathEffect.Style.TRANSLATE);
//
//        PathEffect sum = new SumPathEffect();
//        Path compose = new ComposePathEffect();
//
//        paint.setPathEffect();
//
//        paint.getFillPath();//
//        paint.getTextPath();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        Log.i(TAG, "onLayout: ");
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.i(TAG, "onMeasure: ");
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.i(TAG, "onTouchEvent:  x:" + event.getX() + " y:" + event.getY() + "  width:" + getWidth() + " height:" + getHeight());
        control.x = event.getX() < 0 ? 0 : (event.getX() > getWidth() ? getWidth() : event.getX());
        control.y = event.getY() < 0 ? 0 : (event.getY() > getHeight() ? getHeight() : event.getY());
        invalidate();
        return true;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        Log.i(TAG, "dispatchTouchEvent: ");
        return super.dispatchTouchEvent(event);
    }

}
